<?php

class Admin_StateController extends Zend_Controller_Action {

    public function init() {
        /* Initialize action controller here */
        $adminValidation = new Validations_User();
        $valid = $adminValidation->hasidentity();
        if ($valid) {
            if ($adminValidation->getUserTypeId() != 1) {
                $this->view->message = "You are not admin.";
                Zend_Auth::getInstance()->clearIdentity();
                $this->_redirect('/admin/login');
            }
        } else {
            $this->_redirect('/admin/login');
        }
        $this->_helper->layout->setLayout('admin');
    }

    public function indexAction() {
        $db = new Ads_Model_State();
        $form = new Admin_Form_AddState();
        $this->view->form = $form;
        if ($this->getrequest()->isPost()) {
            if ($form->isValid($this->getRequest()->getPost())) {
                $values = $form->getValues();
                $cname = $values['name'];
                $data = array(
                    'name' => htmlspecialchars($cname)
                );
                $db->insert($data);
            }
        }
        $all = $db->fetchAll();
        $this->view->allState = $all;
    }

    public function updateAction() {
        $stateId = $this->getinputidAction();
        $stateDb = new Ads_Model_State();
        $stateData = $stateDb->getStateById($stateId);
        $form = new Admin_Form_UpdateState();
        $form->main($stateData[0]);
        $this->view->form = $form;
        if ($this->getrequest()->isPost()) {
            if ($form->isValid($this->getRequest()->getPost())) {
                $values = $form->getValues();
                $data = array(
                    'name' => htmlspecialchars($values['name'])
                );
                $stateDb->updateState($data, $values['id']);
                $this->_redirect('/state');
            }
        }
    }

    /**
     * Get State ID.         
     */
    public function getinputidAction() {
        $filters = array(
            'id' => array('HtmlEntities', 'StripTags', 'StringTrim')
        );
        $validators = array(
            'id' => array('Int')
        );
        $input = new Zend_Filter_Input($filters, $validators);
        $input->setData($this->getRequest()->getParams());
        $id = $input->id; //state id
        return $id;
    }

    public function manageAction() {
        $form = new Admin_Form_Manageall();
        $this->view->form = $form;
        if ($this->getRequest()->isPost()) {
            if ($form->isValid($this->getRequest()->getPost())) {
                $values = $form->getValues();                
                $cityDb = new Ads_Model_City();
                $cities = $values['city'];
                foreach ($cities as $city) {
                    $cityDb->updateCity(array('state_id' => $values['state']), $city);
                }
                $this->_redirect("/state/manage");
            }
        }
    }

}

